// pages/wo/shouyiD/shouyiD.js
import request from '../../../utils/http.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    IsLogin: false,
    IsTixian: false, //是否允许提现
    IsDay: false,
    tiXianRiQiTiXing: '',

    dianPuId: 0,
    // 佣金
    isHuiYuan: 2, //会员状态：1是，2否

    zongShouRu: 0, //总输入
    daiTiQu: 0, //
    jianKangHuiTuiJianJiang: 0,
    jianKangHuiFenHong: 0,
    yuanGongJiang: 0,
    daiFenPei: 0,


    tabList: ["佣金明细", "提现记录", "提现"],
    currentTab: 0,

    ruZhangList: [],
    tiQuList: [],
    monthRuZhang: 0, //本月入账
    monthTiQu: 0, //本月出账

    shouKuanRenName: '',
    shouKuanYinHangKaHao: '',
    shouKuanYinHangKaiHuHang: '',
    shouKuanShenFenZhengHao: '',
    shouKuanShouJiHao: '',

    mouth: '',

    windowHeight: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      IsLogin: wx.getStorageSync(app.globalData.LOGINSTATUS),
      dianPuId: wx.getStorageSync(app.globalData.SHOPID),
    })

    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight - (460 / 750 * res.windowWidth),
        })
      },

    })
    //console.log(wx.getStorageSync(app.globalData.screenHeight)-300)

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {

    var date = new Date()
    var zero = '0'
    var mouthStr = date.getMonth() + 1

    if (mouthStr < 10) {
      mouthStr = zero.concat(mouthStr)
    }
    this.setData({
      mouth: date.getFullYear() + '-' + mouthStr
    })
    this.getShouYeMingXi()



  },

  //根据条件，查询收益
  getShouYeMingXi: function () {

    var that = this
    wx.showLoading({
      title: '加载中...',
    })

    let data = {
      dateStr: that.data.mouth,
      dianPuId: that.data.dianPuId,
    }

    request.get('yongJin/personShouYe', data, res => {
      console.log("收益:" + JSON.stringify(res.data))
      that.setData({
        isHuiYuan: res.data.isHuiYuan,
        zongShouRu: res.data.zongShouRu,
        daiTiQu: res.data.daiTiQu,
        daiFenPei: res.data.daiFenPei,
        yuanGongJiang: res.data.yuanGongJiang,
        jianKangHuiTuiJianJiang: res.data.jianKangHuiTuiJianJiang,
        jianKangHuiFenHong: res.data.jianKangHuiFenHong,

        shouKuanRenName: res.data.shouKuanRenName,
        shouKuanYinHangKaHao: res.data.shouKuanYinHangKaHao,
        shouKuanYinHangKaiHuHang: res.data.shouKuanYinHangKaiHuHang,
        shouKuanShenFenZhengHao: res.data.shouKuanShenFenZhengHao,
        shouKuanShouJiHao: res.data.shouKuanShouJiHao,


        ruZhangList: res.data.ruZhangList,
        tiQuList: res.data.tiQuList,
        monthRuZhang: res.data.monthRuZhang,
        monthTiQu: res.data.monthTiQu,
        IsDay: res.data.shiFouKeYiTiXian == 1 ? true : false,
        tiXianRiQiTiXing: res.data.tiXianRiQiTiXing
      })

      that.isReady()
      wx.hideLoading()

    }, err => {
      wx.showToast({
        title: "获取收益信息失败！", // 提示的内容
        icon: "none", // 图标，默认success
        duration: 3000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    })

  },


  //产品分组切换
  onTabsChange(e) {
    let index = e.detail.current == undefined ? e.detail.value : e.detail.current

    if (this.data.currentTab == index) {
      return false;
    } else {
      this.setData({
        currentTab: index
      })
    }
  },


  //月选择器
  bindMouthChange(e) {
    //console.log(e.detail.value)
    this.setData({
      mouth: e.detail.value,
    })
    this.getShouYeMingXi()
  },


  bindNameInput(e) {
    this.setData({
      shouKuanRenName: e.detail.value
    })
    this.isReady()
  },

  bindKaInput(e) {
    this.setData({
      shouKuanYinHangKaHao: e.detail.value
    })
    this.isReady()
  },

  bindHangInput(e) {
    this.setData({
      shouKuanYinHangKaiHuHang: e.detail.value
    })
    this.isReady()
  },

  bindShenInput(e) {
    this.setData({
      shouKuanShenFenZhengHao: e.detail.value
    })
    this.isReady()
  },

  bindPhoneInput(e) {
    this.setData({
      shouKuanShouJiHao: e.detail.value
    })
    this.isReady()
  },


  //判断日期和银行卡完整性，是否可以提现
  isReady() {

    if (this.data.IsDay) {
      if (this.data.shouKuanRenName == '' || this.data.shouKuanRenName == null ||
        this.data.shouKuanYinHangKaHao == '' || this.data.shouKuanYinHangKaHao == null ||
        this.data.shouKuanYinHangKaiHuHang == '' || this.data.shouKuanYinHangKaiHuHang == null ||
        this.data.shouKuanShenFenZhengHao == '' || this.data.shouKuanShenFenZhengHao == null ||
        this.data.shouKuanShouJiHao == '' || this.data.shouKuanShouJiHao == null) {

        this.setData({
          IsTixian: false,
        })
      } else {
        this.setData({
          IsTixian: true,
        })
      }
    } else {
      this.setData({
        IsTixian: false,
      })
    }

  },


  //去提现
  tixian() {

    if (this.data.IsTixian) {
      var that = this
      var data = {
        dianPuId: that.data.dianPuId,
        shouKuanRenName: that.data.shouKuanRenName,
        shouKuanYinHangKaHao: that.data.shouKuanYinHangKaHao,
        shouKuanYinHangKaiHuHang: that.data.shouKuanYinHangKaiHuHang,
        shouKuanShenFenZhengHao: that.data.shouKuanShenFenZhengHao,
        shouKuanShouJiHao: that.data.shouKuanShouJiHao
      }
      request.get('yongJin/tiXian20240321', data, res => {
        wx.hideLoading()
        // console.log("my提取佣金:" + JSON.stringify(res.data))
        if (res.data.status == 1) {
          that.getShouYeMingXi();
          that.setData({
            currentTab: 1,
          })
        } else {
          wx.showToast({
            title: res.data.message || '提现失败，请检查相关信息',
            icon: 'none',
            duration: 2000
          });
        }

      })


    } else {

      if (this.data.IsDay) {
        wx.showToast({
          title: "请填写完整收款卡信息！", // 提示的内容
          icon: "none", // 图标，默认success
          duration: 2000, // 提示的延迟时间，默认1500
          mask: false, // 是否显示透明蒙层，防止触摸穿透
        })
      } else {
        wx.showToast({
          title: this.data.tiXianRiQiTiXing,
          icon: 'none',
          duration: 2000
        });
      }
    }
  },

})